<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>lua.vm.js REPL</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">

    <!-- Le styles -->
    <link href="css/bootstrap.css" rel="stylesheet">
    <style>
      body {
        padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */
      }
    </style>
    <link href="css/bootstrap-responsive.css" rel="stylesheet">

    <!-- codemirror -->
    <script src="js/codemirror/codemirror.js"></script>
    <link rel="stylesheet" href="css/codemirror.css">
    <script src="js/codemirror/lua.js"></script>

  </head>

  <body>

    <div class="navbar navbar-inverse navbar-fixed-top">
      <div class="navbar-inner">
        <div class="container">
          <button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="brand" href="#">lua.vm.js</a>
          <div class="nav-collapse collapse">
            <ul class="nav">
              <li class="active"><a href="repl.html">REPL</a></li>
            </ul>
          </div><!--/.nav-collapse -->
        </div>
      </div>
    </div>

    <div class="container">

      <div class="hero-unit">
        <h2>Lua REPL(交互式解释器)</h2>
        <p>这是一个 Lua 语言的<a href="http://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop">REPL</a> . 点击按钮执行编辑区域中的代码. 你也可以编写你自己的lua代码并运行.</p>
      </div>

      <div class="row">
        <div class="span7" border=1>
          <textarea id="mytext">
print('hello' .. ' ' .. 'world!') -- This is Lua!

print(js.global:eval('[0,1,2,3,4,5][3]')) -- Run JS from Lua

-- Interact with the page using Lua

local screen = js.global.screen
print("you has " .. (screen.width*screen.height) .. " pixels")

local window = js.global -- global object in JS is the window
window:alert("hello from lua!")
window:setTimeout(function() print('hello from lua callback') end, 2500)

local document = js.global.document
print("this window has title '" .. document.title .. "'")

-- call constructors (global, or as properties of other objects)
print("i made an ArrayBuffer of size " .. js.new(js.global.ArrayBuffer, 20).byteLength)
-- print("i made an ArrayBuffer of size " .. js.global.ArrayBuffer:new(20).byteLength)

print("time is " .. js.global.Date.now()) -- call with no arguments

print('done!')
</textarea>
        </div>
        <div class="span5">
          <h4>output</h4>
          <pre id="output"></pre>
        </div>
      </div>

      <p><a href="#" class="btn btn-primary btn-large " onclick="executeLua(myCodeMirror.getValue(), true); return false" id="the_button">Execute &raquo;</a></p>

      <div class="row-fluid">
        <div class="span">
      </div>

    </div> <!-- /container -->

<script>
// CodeMirror
var myCodeMirror = CodeMirror.fromTextArea(document.getElementById('mytext'));
//myCodeMirror.setSize(screen.width*0.6, screen.height*0.2);

// Execution
var outputElement = document.getElementById('output')
var Module = {
  print: function(x) {
    outputElement.textContent = (outputElement.textContent ? outputElement.textContent + '\n' : '') + x;
  }
};

function executeLua(code, clear) {
  if (clear) {
    outputElement.style.color = null;
    outputElement.textContent = '';
  }
  try {
    L.execute(code);
  } catch(e) {
    outputElement.style.color = "red";
    outputElement.textContent = e.toString();
  }
}
</script>

<script src="js/lua.vm.js"></script>

  </body>
</html>

